/*eslint-disable*/
const urlObj = new URL(location.href);
function getQueryString(key) {
  return urlObj.searchParams.get(key);
}

const HK_VIDEO_INDEX = parseInt(getQueryString("index")); // 摄像头详情索引
// const dindex = parseInt(getQueryString('index'));  //
// const CONFIG = window.HK_VIDEO_DATA[dindex].data[HK_VIDEO_INDEX];
const CONFIG = window.VIDEO_DATA[HK_VIDEO_INDEX];
console.log("CONFIG", CONFIG);
// const CONFIG = {
//   'HkDom': 'divPlugin',  // dom元素的id
//   'g_iWndIndex': 0, // 窗口唯一标识索引 //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
//   'wdWidth': 500,
//   'wdHeight': 300,
//   'szIP': '192.168.2.64',
//   'szPort': '80',
//   'iType': '1', // 设置分割的窗口 1x1 = 1， 2x2 = 2， 3x3 = 3， 4x4 = 4
//   'szUsername': 'admin',
//   'szPassword': 'dk16888888'
// };

(() => {
  // // 检查插件是否已经安装过
  // let WVfalg = window.localStorage.getItem('WebComponents'); // 只会弹出一次
  // if (WVfalg) {
  //   // 检查插件是否已经安装过
  //   if (!(-1 == WebVideoCtrl.I_CheckPluginInstall())) {
  //     window.localStorage.setItem('WebComponents', true);
  //   }
  // } else {
  //   alert("您还未安装过插件，双击开发包目录里的WebComponents.exe安装！");
  //   return false;
  // }

  // 初始化插件参数及插入插件
  WebVideoCtrl.I_InitPlugin(490, 330, {
    iWndowType: 2,
    cbSelWnd: function (xmlDoc) {
      CONFIG.g_iWndIndex = $(xmlDoc).find("SelectWnd").eq(0).text();
      var szInfo = "当前选择的窗口编号：" + CONFIG.g_iWndIndex;
      // console.log(szInfo);
    },
  });
  WebVideoCtrl.I_InsertOBJECTPlugin(CONFIG.HkDom);
  WebVideoCtrl.I_ChangeWndNum(1);

  // 检查插件是否最新
  // if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
  //   alert("检测到新的插件版本，双击开发包目录里的WebComponents.exe升级！");
  //   return false;
  // }

  // 登录
  function clickLogin(callback) {
    var szIP = CONFIG.szIP,
      szPort = CONFIG.szPort,
      szUsername = CONFIG.szUsername,
      szPassword = CONFIG.szPassword;

    if ("" == szIP || "" == szPort) {
      return;
    }

    var iRet = WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
      success: function (xmlDoc) {
        console.log(szIP + " 登录成功！");
        callback && callback();
      },
      error: function () {
        console.log(szIP + " 登录失败！");
      },
    });

    if (-1 == iRet) {
      console.log(szIP + " 已登录过！");
    }
  }

  // 开始预览
  function clickStartRealPlay() {
    // debugger;
    // console.log(WebVideoCtrl);
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex),
      szIP = CONFIG.szIP,
      // iStreamType = 1, 2, 3, 4 分别代表流 主码流 子码流 第三码流 转码码流
      iStreamType = parseInt(2, 10),
      // 1 代表 IPdome
      iChannelID = parseInt(1, 10),
      bZeroChannel = true, // 默认false
      szInfo = "";

    if ("" == szIP) {
      return;
    }

    if (oWndInfo != null) {
      // 已经在播放了，先停止
      WebVideoCtrl.I_Stop();
    }

    var iRet = WebVideoCtrl.I_StartRealPlay(szIP, {
      iStreamType: iStreamType,
      iChannelID: iChannelID,
      bZeroChannel: bZeroChannel,
    });

    if (0 == iRet) {
      szInfo = "开始预览成功！";
    } else {
      szInfo = "开始预览失败！";
    }

    // console.log(szIP + " " + szInfo);
    const dom = document.querySelector(".dkname");
    dom.textContent  = CONFIG.title;
  }

  // 登录成功后开始播放
  clickLogin(() => {
    setTimeout((_) => {
      iType = parseInt(CONFIG.iType, 10);
      // WebVideoCtrl.I_ChangeWndNum(iType);
      clickStartRealPlay();
    }, 3000);
  });

  // 鼠标控制点
  // 全屏
})();

function clickFullScreen() {
  WebVideoCtrl.I_FullScreen(true);
}

const mouseDownPTZControl = (iPTZIndex) => {
  // PTZ控制 9为自动，1,2,3,4,5,6,7,8为方向PTZ
  var g_bPTZAuto = false;
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex),
    bZeroChannel = false,
    iPTZSpeed = 4, // 云台速度可以设置 1~7
    bStop = false;

  if (bZeroChannel) {
    // 零通道不支持云台
    return;
  }

  if (oWndInfo != null) {
    if (9 == iPTZIndex && g_bPTZAuto) {
      iPTZSpeed = 0; // 自动开启后，速度置为0可以关闭自动
      bStop = true;
    } else {
      g_bPTZAuto = false; // 点击其他方向，自动肯定会被关闭
      bStop = false;
    }

    WebVideoCtrl.I_PTZControl(iPTZIndex, bStop, {
      iPTZSpeed: iPTZSpeed,
      success: function (xmlDoc) {
        if (9 == iPTZIndex) {
          g_bPTZAuto = !g_bPTZAuto;
        }
        console.log(oWndInfo.szIP + " 开启云台成功！");
      },
      error: function () {
        console.log(oWndInfo.szIP + " 开启云台失败！");
      },
    });
  }
};

// 方向PTZ停止
const mouseUpPTZControl = () => {
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex);

  if (oWndInfo != null) {
    WebVideoCtrl.I_PTZControl(1, true, {
      success: function (xmlDoc) {
        console.log(oWndInfo.szIP + " 停止云台成功！");
      },
      error: function () {
        console.log(oWndInfo.szIP + " 停止云台失败！");
      },
    });
  }
};

/**
 * 遮住插件窗口
 * left 距离插件窗口左边距
 * top 距离插件窗口上边距
 * width 需要扣除的宽度
 * height 需要扣除的高度
 */
function hkVideoCover(left = 0, top = 0) {
  const width = document.getElementById("divPlugin").offsetWidth + 1;
  const height = document.getElementById("divPlugin").offsetHeight + 1;
  if (WebVideoCtrl !== null) {
    window.onresize = null;
    window.onscroll = null;
    WebVideoCtrl.JS_CuttingPartWindow(left, top, width, height);
  }
}

/**
 * 还原插件窗口
 */
function hkVideoRestore(left = 0, top = 0) {
  const width = document.getElementById("divPlugin").offsetWidth + 1;
  const height = document.getElementById("divPlugin").offsetHeight + 1;
  if (WebVideoCtrl !== null) {
    WebVideoCtrl.JS_RepairPartWindow(left, top, width, height);
  }
}

// 打开声音
function openSound() {
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex),
    szInfo = "";

  if (oWndInfo != null) {
    var allWndInfo = WebVideoCtrl.I_GetWindowStatus();
    // 循环遍历所有窗口，如果有窗口打开了声音，先关闭
    for (var i = 0, iLen = allWndInfo.length; i < iLen; i++) {
      oWndInfo = allWndInfo[i];
      if (oWndInfo.bSound) {
        WebVideoCtrl.I_CloseSound(oWndInfo.iIndex);
        break;
      }
    }

    var iRet = WebVideoCtrl.I_OpenSound();

    if (0 == iRet) {
      szInfo = "打开声音成功！";
      const dom1 = document.querySelector('.opendk');
      const dom2 = document.querySelector('.stopdk');
      dom1.style.display = "none";
      dom2.style.display = "block";
      const rangeInput = document.getElementById('h5_volume');
        // 注册事件监听器
        rangeInput.addEventListener('input', function(event) {
            // 获取参数的值
            var value = event.target.value;
            // 在控制台输出值
            // console.log(value);
            setVolume(value);

        });
    } else {
      szInfo = "打开声音失败！";
    }
    console.log(szInfo);
  }
}
// 关闭声音
function closeSound() {
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex),
    szInfo = "";

  if (oWndInfo != null) {
    var iRet = WebVideoCtrl.I_CloseSound();
    if (0 == iRet) {
      szInfo = "关闭声音成功！";
      const dom1 = document.querySelector('.opendk');
      const dom2 = document.querySelector('.stopdk');
      dom1.style.display = "block";
      dom2.style.display = "none";
    } else {
      szInfo = "关闭声音失败！";
    }
    console.log(szInfo);
  }
}
// 设置音量
function setVolume(num) {
  var oWndInfo = WebVideoCtrl.I_GetWindowStatus(CONFIG.g_iWndIndex),
    iVolume = Number(num),
    szInfo = "";
  if (oWndInfo != null) {
    var iRet = WebVideoCtrl.I_SetVolume(iVolume);
    console.log("iRet",iRet);
    if (0 == iRet) {
      szInfo = "音量设置成功！";
    } else {
      szInfo = "音量设置失败！";
    }
    console.log(szInfo);
  }
}
